//
//  FFSettingViewController.swift
//  Clock
//
//  Created by jisa on 2020/5/7.
//  Copyright © 2020 jff. All rights reserved.
//

import UIKit

class FFFontSettingViewController: UIViewController {
    
    fileprivate var list:[String]!
    fileprivate var fontTableView:UITableView!
    fileprivate var selectedIndex:Int = 0
    fileprivate var completeBtn:UIButton!
    fileprivate var titleLabel:UILabel!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        list = FFSystemSupportFont()
        
        let selectFontName = FFStoreHelper.share.FFGetSelectFont()
        for i in 0 ..< list.count {
            if selectFontName == list[i] {
                selectedIndex = i
                break
            }
        }
        FFSetUpTableView()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        fontTableView.frame = view.bounds
        titleLabel.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 50 * ScaleOf375)
        completeBtn.frame = CGRect(x: view.bounds.size.width - 60 * ScaleOf375, y: 10 * ScaleOf375, width: 60 * ScaleOf375, height: 30 * ScaleOf375)

    }
}

extension FFFontSettingViewController {
    func FFSystemSupportFont() -> Array<String> {
        var arr:Array = Array<String>();
        for family in UIFont.familyNames {
            for name in UIFont.fontNames(forFamilyName: family) {
                arr.append(name)
                break
            }
        }
        arr.insert(SystemFontName, at: 0)
        return arr
    }
}

extension FFFontSettingViewController:UITableViewDelegate, UITableViewDataSource {
        
    func FFSetUpTableView() {
        fontTableView = UITableView(frame: .zero, style: .grouped)
        fontTableView.delegate = self
        fontTableView.dataSource = self
        fontTableView.rowHeight = 60 * ScaleOf375
        fontTableView.tableHeaderView = FFSetUpTableHeaderView()
        fontTableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "default")
        
        view.addSubview(fontTableView)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "default")!
        cell.textLabel!.text = "11:03"//list[indexPath.row]
        cell.textLabel!.font = UIFont(name: list[indexPath.row], size: 25)
        cell.selectionStyle = .none
        cell.accessoryView = indexPath.row == selectedIndex ? FFAcquireAccessoryView() : nil
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row != selectedIndex {
            let preCell:UITableViewCell? = tableView.cellForRow(at: IndexPath(row: selectedIndex, section: 0))
            preCell?.accessoryView = nil
            
            let cell:UITableViewCell = tableView.cellForRow(at: indexPath)!
            selectedIndex = indexPath.row
            cell.accessoryView = FFAcquireAccessoryView()
        }
    }
    
    private func FFAcquireAccessoryView() -> UIView {
        let imageView:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        imageView.image = UIImage(named: "selected")
        return imageView
//        let vi:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
//        vi.backgroundColor = UIColor.init(patternImage: UIImage(named: "selected")!)
//        return vi
    }
    
    fileprivate func FFSetUpTableHeaderView() -> UIView {
        let vi = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 60 * ScaleOf375))
        vi.backgroundColor = UIColor.white
        
        titleLabel = UILabel(frame: vi.bounds)
        titleLabel.text = "字体选择"
        titleLabel.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        titleLabel.textAlignment = .center
        vi.addSubview(titleLabel)

        completeBtn = UIButton(type: .custom)
        completeBtn.setTitle("完成", for: .normal)
        completeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        completeBtn.setTitleColor(UIColor.black, for: .normal)
        completeBtn.addTarget(self, action: #selector(FFCanclePresent), for: .touchUpInside)
        vi.addSubview(completeBtn)
        
        return vi
    }
    
    @objc fileprivate func FFCanclePresent() {
        FFStoreHelper.share.FFSetUpSelectFont(font: list[selectedIndex])
        dismiss(animated: true) {
            NotificationCenter.default.post(Notification(name: Notification.Name.init("UpdateFont")))
        }
    }
    
}
